import 'package:flutter/material.dart';

///
/// 含成功/失败的图片 toast
///
class SuccessToast {
  static OverlayEntry? overlayEntry;

  static final SuccessToast _showToast = SuccessToast._internal();

  factory SuccessToast() {
    return _showToast;
  }

  SuccessToast._internal();

  static toast(
    context,
    String text,
    String imgPath,
  ) {
    if (overlayEntry != null) return;
    overlayEntry = OverlayEntry(builder: (context) {
      return Container(
        color: const Color(0x4A000000),
        child: Center(
          child: Container(
            width: 204,
            height: 384 / 2,
            padding: const EdgeInsets.fromLTRB(21 / 2, 29 / 2, 21 / 2, 42 / 2),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(7),
            ),
            child: Stack(
              alignment: Alignment.bottomCenter,
              children: [
                Image.asset(
                  imgPath,
                  width: 366 / 2,
                  height: 313 / 2,
                ),
                Text(
                  text,
                  textAlign: TextAlign.center,
                  style:
                      const TextStyle(fontSize: 15, color: Color(0xFF606972)),
                )
              ],
            ),
          ),
        ),
      );
    });
    var overlayState = Overlay.of(context);
    overlayState!.insert(overlayEntry!);
    Future.delayed(const Duration(seconds: 3), () {
      overlayEntry!.remove();
      overlayEntry = null;
    });
  }

  static show(
    context,
    String text,
    String imgPath, {
    Function? delayedCallback,
  }) {
    if (overlayEntry != null) return;
    overlayEntry = OverlayEntry(builder: (context) {
      return Container(
        color: const Color(0x4A000000),
        child: Center(
          child: Stack(
            alignment: Alignment.topCenter,
            children: [
              Container(
                width: 362 / 2,
                height: 340 / 2,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(7),
                    color: Colors.white),
                padding:
                    const EdgeInsets.fromLTRB(30 / 2, 182 / 2, 50 / 2, 22 / 2),
                child: Row(
                  children: [
                    Expanded(
                      child: Text(
                        text,
                        textAlign: TextAlign.center,
                        style: const TextStyle(
                            fontSize: 15, color: Color(0xFF606972)),
                      ),
                    )
                  ],
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: 11),
                child: Image.asset(
                  imgPath,
                  width: 362 / 2,
                  height: 138 / 2,
                ),
              ),
            ],
          ),
        ),
      );
    });
    var overlayState = Overlay.of(context);
    overlayState!.insert(overlayEntry!);
    Future.delayed(const Duration(seconds: 3), () {
      overlayEntry!.remove();
      overlayEntry = null;
      if (delayedCallback != null) {
        delayedCallback();
      }
    });
  }
}
